<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false" width="700px"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="140px">
      <el-form-item label="车牌号" prop="numberPlate">
        <el-input v-model="dataForm.numberPlate" placeholder="车牌号" style="width: 480px;"></el-input>
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input v-model="dataForm.brand" placeholder="品牌" style="width: 480px;"></el-input>
      </el-form-item>
      <el-form-item label="型号" prop="model">
        <el-input v-model="dataForm.model" placeholder="型号" style="width: 480px;"></el-input>
      </el-form-item>
       <el-form-item label="限速（km/h）" prop="limitSpeed">
        <el-input v-model="dataForm.limitSpeed" placeholder="情输入限定速度" style="width: 480px;"></el-input>
      </el-form-item>
       <el-form-item label="GPS平台编号" prop="clientId">
        <el-input v-model="dataForm.clientId" placeholder="请输入GPS平台编号" style="width: 480px;"></el-input>
      </el-form-item>
       <el-form-item label="设备编号" prop="deviceNo">
        <el-input v-model="dataForm.deviceNo" placeholder="请输入设备编号" style="width: 480px;"></el-input>
      </el-form-item>
      <el-form-item label="收运单位" prop="ctUnitId">
        <el-select v-model="dataForm.ctUnitId" placeholder="请选择收运单位" style="width: 480px;" @change="unitChange">
            <el-option v-for="unit in unitList" :key="unit.id" :label="unit.name" :value="unit.id">
            </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车队" prop="fleetId">
        <el-select v-model="dataForm.fleetId" placeholder="请选择车队" style="width: 480px;">
          <template v-for="fleet in fleetList">
            <el-option v-if="fleet.ctUnitId === dataForm.ctUnitId" :key="fleet.id" :label="fleet.fleetName" :value="fleet.id">
            </el-option>
          </template>
        </el-select>
      </el-form-item>
      <el-form-item label="司机" prop="driverId">
        <el-select v-model="dataForm.driverId" placeholder="请选择" style="width: 480px;">
          <template v-for="user in userList">
            <el-option v-if="user.ctUnitId === dataForm.ctUnitId" :key="user.userId" :label="user.username" :value="user.userId">
              <span>{{ user.username }}</span><span style="float: right;">{{ user.mobile }}</span>
            </el-option>
          </template>
        </el-select>
      </el-form-item>
      <el-form-item label="污水罐容积" prop="carCapacity">
        <el-input v-model="dataForm.carCapacity" oninput="value=value.replace(/[^0-9.]/g,'')" placeholder="车辆污水罐容积" style="width: 480px;">
          <template slot="append">吨</template>
        </el-input>
      </el-form-item>
      <!-- <el-form-item label="当前液位量" prop="currentLiquidValue">
        <el-input v-model="dataForm.currentLiquidValue" oninput="value=value.replace(/[^0-9.]/g,'')" placeholder="当前液位量" style="width: 480px;">
          <template slot="append">吨</template>
        </el-input>
      </el-form-item> -->
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="dataForm.remark" placeholder="备注" maxlength="180" show-word-limit style="width: 480px;"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    unitList: {
      type: Array,
      default: () => []
    },
    fleetList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      visible: false,
      userList: [],
      dataForm: {
        id: 0,
        brand: '',
        model: '',
        numberPlate: '',
        ctUnitId: '',
        fleetId: '',
        driverId: '',
        carCapacity: '',
        limitSpeed: '',
        clientId: '',
        deviceNo:"",
        remark: ''
      },
      dataRule: {
        ctUnitId: [
          { required: true, message: '收运单位不能为空', trigger: 'change' }
        ],
        brand: [
          { required: false, message: '品牌不能为空', trigger: 'blur' }
        ],
        model: [
          { required: false, message: '型号不能为空', trigger: 'blur' }
        ],
        numberPlate: [
          { required: true, message: '车牌号不能为空', trigger: 'blur' }
        ],
        fleetId: [
          { required: true, message: '车队不能为空', trigger: 'change' }
        ],
        driverId: [
          { required: true, message: '司机不能为空', trigger: 'change' }
        ],
        carCapacity: [
          { required: true, message: '车辆污水罐容积不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    this.getUserListByRoleName('司机')
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sign/signcar/getCarBasicInfo/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data.signCar && data.code === 0) {
              this.dataForm.ctUnitId = data.signCar.ctUnitId
              this.dataForm.brand = data.signCar.brand
              this.dataForm.model = data.signCar.model
              this.dataForm.numberPlate = data.signCar.numberPlate
              this.dataForm.fleetId = data.signCar.fleetId
              this.dataForm.driverId = data.signCar.driverId
              this.dataForm.carCapacity = data.signCar.carCapacity
              this.dataForm.limitSpeed = data.signCar.limitSpeed
              this.dataForm.remark = data.signCar.remark
              this.dataForm.clientId = data.signCar.clientId
              this.dataForm.deviceNo = data.signCar.deviceNo
          
            }
          })
        }
      })
    },
    getUserListByRoleName (roleName) {
      this.$http({
        url: this.$http.adornUrl(`/sys/user/rolename`),
        method: 'post',
        data: this.$http.adornData({ roleName: roleName })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.userList = data.list || []
        } else {
          this.userList = []
        }
      })
    },
    unitChange () {
      this.dataForm.fleetId = ''
      this.dataForm.driverId = ''
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sign/signcar/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'ctUnitId': this.dataForm.ctUnitId,
              'brand': this.dataForm.brand,
              'model': this.dataForm.model,
              'numberPlate': this.dataForm.numberPlate,
              'fleetId': this.dataForm.fleetId,
              'driverId': this.dataForm.driverId,
              'carCapacity': this.dataForm.carCapacity,
              'limitSpeed': this.dataForm.limitSpeed,
              'remark': this.dataForm.remark,
              'clientId': this.dataForm.clientId,
              'deviceNo': this.dataForm.deviceNo
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
